package com.zwz.mapper;


import com.zwz.entity.EnterpriseInfo;
import com.zwz.voObject.EnterpriseBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface EnterpriseMapper {
    @Insert(" INSERT INTO enterprise_info (\n" +
            "         id,   company_name, company_profile, establish_date, business_status,\n" +
            "            registered_capital, currency_type, subscribed_capital, subscription_date,\n" +
            "            paid_in_capital, registered_address, detail_address, industry_category,\n" +
            "            business_scope, company_type, institution_type, is_state_owned, former_names,\n" +
            "            revocation_date, cancellation_date, has_business_license, abolish_declaration\n" +
            "        ) " +
            "VALUES(   #{id},#{companyName}, #{companyProfile}, #{establishDate}, #{businessStatus},\n" +
            "                     #{registeredCapital}, #{currencyType}, #{subscribedCapital}, #{subscriptionDate},\n" +
            "                     #{paidInCapital}, #{registeredAddress}, #{detailAddress}, #{industryCategory},\n" +
            "                     #{businessScope}, #{companyType}, #{institutionType}, #{isStateOwned}, #{formerNames},\n" +
            "                     #{revocationDate}, #{cancellationDate}, #{hasBusinessLicense}, #{abolishDeclaration}   )" )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(EnterpriseInfo enterprise);

    @Select("SELECT * FROM enterprise_info WHERE id = #{id}")
    EnterpriseInfo selectById(Long id);

    @Update("UPDATE enterprise_info SET company_name=#{companyName}, business_status=#{businessStatus} ... WHERE id=#{id}")
    int update(EnterpriseInfo enterprise);

    @Delete("DELETE FROM enterprise_info WHERE id = #{id}")
    int delete(Long id);

    @Select("SELECT * FROM enterprise_info")
    List<EnterpriseInfo> selectAll();

    @Select({
            "<script>",
            "SELECT " +
                    "  t1.company_name as companyName, " +
                    "  t1.establish_date as establishDate, " +
                    "  t1.company_type, " +
                    "  t1.registered_address as registeredAddress," +
                    "  t2.mobile " +
                    "FROM enterprise_info t1 " +
                    "LEFT JOIN agent_account_info t2 ON t1.id = t2.enterprise_id " +
                    "<where> " +
                    "  <if test='companyName != null and companyName != \"\"'> " +
                    "    AND t1.company_name LIKE CONCAT('%', #{companyName}, '%') " +
                    "  </if> " +
                    "</where>",
            "</script>"
    })
    List<EnterpriseBean> selectAllBean(String companyName);


    @Select({
            "<script>",
            "SELECT " +
                    "  t1.company_name as companyName, " +
                    "  t1.establish_date as establishDate, " +
                    "  t1.company_type, " +
                    "  t1.registered_address as registeredAddress," +
                    "  t2.mobile " +
                    "FROM enterprise_info t1 " +
                    "LEFT JOIN agent_account_info t2 ON t1.id = t2.enterprise_id " +
                    "<where> " +
                    "  <if test='companyType != null and companyType != \"\"'> " +
                    "    AND t1.company_type LIKE CONCAT('%', #{companyType}, '%') " +
                    "  </if> " +
                    "</where>",
            "</script>"
    })
    List<EnterpriseBean> selectAllBeanByCompanyType(String companyType);


    @Select({
            "<script>",
            "SELECT " +
                    "  t1.company_name as companyName, " +
                    "  t1.establish_date as establishDate, " +
                    "  t1.company_type, " +
                    "  t1.registered_address as registeredAddress," +
                    "  t2.mobile " +
                    "FROM enterprise_info t1 " +
                    "LEFT JOIN agent_account_info t2 ON t1.id = t2.enterprise_id " +
                    "<where> " +
                    "  <if test='startDate != null and startDate != \"\"'> " +
                    "     t1.establish_date between #{startDate}  and  #{endDate} " +
                    "  </if> " +
                    "</where>",
            "</script>"
    })
    List<EnterpriseBean> selectAllBeanByByDate(@Param("startDate") String startDate, @Param("endDate") String endDate);


    @Select({
            "<script>",
            "SELECT " +
                    "  t1.company_name as companyName, " +
                    "  t1.establish_date as establishDate, " +
                    "  t1.company_type, " +
                    "  t1.registered_address as registeredAddress," +
                    "  t2.mobile " +
                    "FROM enterprise_info t1 " +
                    "LEFT JOIN agent_account_info t2 ON t1.id = t2.enterprise_id " +
                    "<where> " +
                    "  <if test='businessStatus != null and businessStatus != \"\"'> " +
                    "    AND t1.business_status LIKE CONCAT('%', #{businessStatus}, '%') " +
                    "  </if> " +
                    "</where>",
            "</script>"
    })
    List<EnterpriseBean> selectAllBeanByBusinessStatus(String businessStatus);


    /**
     * 分页查询企业信息
     *
     * @param offset   偏移量
     * @param pageSize 每页大小
     * @return 企业信息列表
     */
    List<EnterpriseBean> selectPageBean(@Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    /**
     * 获取总记录数
     * @return 总记录数
     */
    Long selectTotalCount();
}